<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/23
 * Time: 16:52
 */

namespace app\api\controller;


use think\Db;

class Shop extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    private $order_number;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $this->order_number = date('YmdHis') . rand(1000, 9999);
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 一级分类
     * @return \think\response\Json
     */
    public function firstCate(){
        $cate = Db::table('up_cate')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->order('cate_sort asc')
            ->field('cate_id, cate_name')
            ->select();
        if($cate){
            return json(['status' => 200, 'msg' => $cate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 获取二级分类
     * @return \think\response\Json
     * cate_type    一级分类还是热销   1 一级分类    2 热销
     * f_cate_id    一级分类ID
     * page         页码
     */
    public function secondCate(){
        $cate_type = input('cate_type');
        if($cate_type == 1){
            $f_cate_id = input('f_cate_id');
            $list = Db::table('up_cate')
                ->where('cate_status', 1)
                ->where('cate_fid', $f_cate_id)
                ->order('cate_sort asc')
                ->field('cate_id, cate_name, cate_icon')
                ->select();
        }else{
            $where['goods_status'] = 1;
            $where['goods_del'] = 1;
            $where['goods_is_hot'] = 1;
            $list = Db::table('up_goods')
                ->field('goods_id, goods_thumb, goods_name')
                ->where($where)
                ->order('goods_time decs')
//                ->page($this->page, 10)
                ->select();
        }
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 分类商品列表
     * @return \think\response\Json
     * s_cate           二级分类ID
     */
    public function goods(){
        $params = input('');
        $where['goods_status'] = 1;
        $where['goods_del'] = 1;
        $where['goods_s_id'] = $params['s_cate'];
        $goods = Db::table('up_goods')
            ->field('goods_id, goods_thumb, goods_name')
            ->where($where)
            ->order('goods_time decs')
//            ->page($this->page, 10)
            ->select();
        if($goods){
            return json(['status' => 200, 'msg' => $goods]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 热门搜索
     * @return \think\response\Json
     */
    public function hotSearch(){
        $hot_search = Db::table('up_hot_search')
            ->where('search_status', 1)
            ->order('search_time desc')
            ->field('search_id, search_word')
            ->select();
        if($hot_search){
            return json(['status' => 200, 'msg' => $hot_search]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 搜索商品列表
     * @return \think\response\Json
     * search_order    排序  1 价格降序  2 价格升序  3 销量降序  4 销量升序
     * search_type     搜索类型   1 二级分类下搜索   2 热门分类下搜索  3 全部分类
     * search_word     关键字
     * search_id       热门搜索ID
     * s_cate_id       二级分类ID
     */
    public function searchGoods(){
        $params = input('');
        
        // 排序
        $order = 'goods_time desc';
        // 1 价格降序  2 价格升序   3 销量降序   4 销量升序
        if(isset($params['search_order'])){
            if($params['search_order'] == 1) $order = 'goods_price desc';
            if($params['search_order'] == 2) $order = 'goods_price asc';
            if($params['search_order'] == 3) $order = 'goods_sale_num desc';
            if($params['search_order'] == 4) $order = 'goods_sale_num asc';
        }
        
        // 条件
        $where['goods_status'] = 1;
        $where['goods_del'] = 1;
        if(!empty($params['search_word'])){
            $where['goods_name'] = ['like', '%' . $params['search_word'] . '%'];
        }
        if($params['search_type'] == 1){ // 二级分类下
            $where['goods_s_id'] = $params['s_cate_id'];
        }elseif($params['search_type'] == 2){ // 热门搜索下
            $goods_id = Db::table('up_hot_search')->where('search_id', $params['search_id'])->value('search_goods_id');
            $where['goods_id'] = ['IN', $goods_id];
        }
        
        $goods = Db::table('up_goods')
            ->field('goods_id, goods_name, goods_thumb, goods_price')
            ->where($where)
            ->order($order)
//            ->page($this->page, 10)
            ->select();
        if($goods){
            return json(['status' => 200, 'msg' => $goods]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 商品详情
     * @return \think\response\Json
     * goods_id    商品ID
     */
    public function goodsDetail(){
        header("Access-Control-Allow-Origin: *");
        $goods_id = input('goods_id');
        $info = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_id, goods_images, goods_name, goods_price, goods_stock, goods_intro, 
            goods_serve, goods_thumb')
            ->find();
        // 是否收藏
        $is_collect = Db::table('up_collect')
            ->where('collect_user_id', $this->user_id)
            ->where('collect_goods_id', $goods_id)
            ->find();
        if($is_collect){
            $info['collect'] = 1; // 已收藏
        }else{
            $info['collect'] = 2; // 未收藏
        }
        $info['goods_images'] = explode(',', trim($info['goods_images'], ','));
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 收藏操作
     * @return \think\response\Json
     * goods_id
     */
    public function collect(){
        $goods_id = input('goods_id');
        $have = Db::table('up_collect')
            ->where('collect_user_id', $this->user_id)
            ->where('collect_goods_id', $goods_id)
            ->find();
        if($have){ // 已经收藏
            $res = Db::table('up_collect')
                ->where('collect_user_id', $this->user_id)
                ->where('collect_goods_id', $goods_id)
                ->delete();
        }else{ // 未收藏
            $insert = [
                'collect_user_id' => $this->user_id,
                'collect_goods_id' => $goods_id,
                'collect_time' => $this->create_time
            ];
            $res = Db::table('up_collect')->insert($insert);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 确认订单页面
     * @return \think\response\Json
     * goods_id     订单ID
     * address_id   收货地址ID
     */
    public function confirmShow(){
        $params = input('');
        $goods = Db::table('up_goods')
            ->where('goods_id', $params['goods_id'])
            ->field('goods_id, goods_name, goods_thumb, goods_price, goods_stock, goods_freight')
            ->find();
        // 运费
        $freight = $this->freight($params['address_id'], $goods['goods_freight']);
        $goods['is_free'] = $freight;
        return json(['status' => 200, 'msg' => $goods]);
    }
    
    /**
     * 根据用户选择的收货地址，改变商品是否包邮
     * @return \think\response\Json
     * goods_id    商品ID
     * address_id  收货地址ID
     */
    public function freByAddr(){
        $params = input('');
        $goods_fre = Db::table('up_goods')->where('goods_id', $params['goods_id'])->value('goods_freight');
        $is_free = $this->freight($params['address_id'], $goods_fre);
        return json(['status' => 200, 'msg' => $is_free]);
    }
    
    /**
     * 结算/创建订单
     * @return \think\response\Json
     * order_num        订单数量
     * order_goods_id   商品ID
     * order_addr_id    收货地址ID
     */
    public function settle(){
        $params = input('');
        // 商品信息
        $goods = Db::table('up_goods')
            ->where('goods_id', $params['order_goods_id'])
            ->field('goods_freight, goods_price, goods_stock, goods_thumb')
            ->find();
        if($params['order_num'] > $goods['goods_stock']){
            return json(['status' => 0, 'msg' => '库存不足']);
        }
        
        // 收货地址
        $address = Db::table('up_my_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
            ->field('addr_user_name, addr_phone, addr_address, ua1.district addr_sheng, 
            ua2.district addr_shi, ua3.district addr_qu')
            ->where('addr_id', $params['order_addr_id'])
            ->find();
        // 运费
        $freight = $this->freight($params['order_addr_id'], $goods['goods_freight']);
        // 订单编号
        $order_number = date('YmdHis') . rand(1000, 9999);//订单编号
        // 订单总价
        $total = $goods['goods_price'] * $params['order_num'];
    
        $insert = [
            'order_user_id' => $this->user_id,
            'order_goods_id' => $params['order_goods_id'],
            'order_number' => $order_number,
            'order_num' => $params['order_num'],
            'order_price' => $total,
            'order_freight' => $freight,
            'order_time' => $this->create_time,
            'order_user_phone' => $address['addr_phone'],
            'order_user_name' => $address['addr_user_name'],
            'order_sheng' => $address['addr_sheng'],
            'order_shi' => $address['addr_shi'],
            'order_qu' => $address['addr_qu'],
            'order_addrerss' => $address['addr_address'],
            'order_goods_thumb' => $goods['goods_thumb']
        ];
        
        $res = Db::table('up_order')->insertGetId($insert);
        if($res){
            // 减库存
            Db::table('up_goods')->where('goods_id', $params['order_goods_id'])->setDec('goods_stock', $params['order_num']);
            return json(['status' => 200, 'msg' => $res]);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    
}